package com.wantupai.translate.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wantupai.translate.R;

/**
 * 项目名称：wtp
 * 创建人：luxuchang
 * 创建时间：2018/12/20  下午 05:16
 * 描述：
 */
public class SspShareTypePop extends PopupWindow implements View.OnClickListener {

    private Activity activity;
    private OnBtnClickListener listener;

    @SuppressLint("WrongConstant")
    public SspShareTypePop(Activity activity, OnBtnClickListener listener) {
        this.activity = activity;
        this.listener = listener;
        View view = View.inflate(activity.getApplicationContext(), R.layout.layout_share_pop_type, null);
        FindView(view);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(false);
        setFocusable(true);
        setContentView(view);
    }

    private void FindView(View v) {
        TextView share_type_txt = v.findViewById(R.id.share_tp_txt);
        TextView share_type_pic = v.findViewById(R.id.share_tp_pic);
        TextView share_type_pdf = v.findViewById(R.id.share_tp_pdf);
        TextView share_type_cancle = v.findViewById(R.id.share_tp_cancel);
        share_type_txt.setOnClickListener(this);
        share_type_pic.setOnClickListener(this);
        share_type_pdf.setOnClickListener(this);
        share_type_cancle.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.share_tp_cancel) {
            dismiss();
        } else if (i == R.id.share_tp_txt) {
            if (listener != null) listener.OnBtnClick(0);
            dismiss();
        } else if (i == R.id.share_tp_pic) {
            if (listener != null) listener.OnBtnClick(1);
            dismiss();
        } else if (i == R.id.share_tp_pdf) {
            if (listener != null) listener.OnBtnClick(2);
            dismiss();
        }
    }


    public void show() {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.3f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);
        showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }


    @Override
    public void dismiss() {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 1.0f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);
        super.dismiss();
    }

    public interface OnBtnClickListener {
        void OnBtnClick(int type);
    }
}
